package ua.com.minus1.lookbattle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import ua.com.minus1.lookbattle.utils.BundleCreator;
import ua.com.minus1.lookbattle.utils.BundleValues;

/**
 * Created with IntelliJ IDEA.
 * User: Igorek
 * Date: 10/31/12
 * Time: 9:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class StartFragment extends SherlockFragment {

    private GridView gridView;

    public static StartFragment newInstance() {
        return new StartFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_start, container, false);
        gridView = (GridView) viewGroup.findViewById(R.id.start_grid_view);
        return gridView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);


    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupViews();
    }

    private void setupViews() {
        CatItemFactory.CatItem[] catItems = CatItemFactory.createItems(getActivity());
        gridView.setAdapter(new CatsGridAdapter(catItems));
        gridView.setOnItemClickListener(new GridItemClickListener());
    }

    private class CatsGridAdapter extends ArrayAdapter<CatItemFactory.CatItem> {
        private CatsGridAdapter(CatItemFactory.CatItem[] catItems) {
            super(getActivity(),R.layout.cat_grid_item, catItems);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.cat_grid_item, null);
            }

            CatItemFactory.CatItem catItem = getItem(position);

            TextView textView = (TextView) convertView.findViewById(R.id.catGridText);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, catItem.icon, null, null);
            textView.setText(catItem.label);

            return convertView;
        }
    }

    private class GridItemClickListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            CatsGridAdapter adapter = (CatsGridAdapter) adapterView.getAdapter();

            Bundle extras = new Bundle();
            extras.putInt(BundleValues.CAT_ID, adapter.getItem(i).catId);

            Intent intent = new Intent(getActivity(), BattleActivity.class);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
